{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sympy as sm\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from chempy import ReactionSystem\n",
    "from chempy.units import to_unitless, SI_base_registry as si, default_units as u, default_constants as const\n",
    "from chempy.kinetics.ode import get_odesys\n",
    "from chempy.kinetics.integrated import binary_rev\n",
    "sm.init_printing()\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "R = 8.314472\n",
    "T_K = 273.15 + 20  # 20 degree celsius\n",
    "kB = 1.3806504e-23\n",
    "h = 6.62606896e-34\n",
    "\n",
    "dHf = 74e3\n",
    "dSf = R*np.log(h/kB/T_K*1e16)\n",
    "\n",
    "dHb = 79e3\n",
    "dSb = dSf - 23\n",
    "\n",
    "rsys1 = ReactionSystem.from_string(\"\"\"\n",
    "Fe+3 + SCN- -> FeSCN+2; EyringParam(dH={dHf}*J/mol, dS={dSf}*J/K/mol)\n",
    "FeSCN+2 -> Fe+3 + SCN-; EyringParam(dH={dHb}*J/mol, dS={dSb}*J/K/mol)\n",
    "\"\"\".format(dHf=dHf, dSf=dSf, dHb=dHb, dSb=dSb))\n",
    "kf_ref = 20836643994.118652*T_K*np.exp(-(dHf - T_K*dSf)/(R*T_K))\n",
    "kb_ref = 20836643994.118652*T_K*np.exp(-(dHb - T_K*dSb)/(R*T_K))\n",
    "kf_ref, kb_ref"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Fe0 = 6e-3\n",
    "SCN0 = 2e-3\n",
    "init_cond = {\n",
    "    'Fe+3': Fe0*u.M,\n",
    "    'SCN-': SCN0*u.M,\n",
    "    'FeSCN+2': 0*u.M\n",
    "}\n",
    "t = 3*u.second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def integrate_and_plot(rsys, params):\n",
    "    odes, extra = get_odesys(rsys, include_params=False, unit_registry=si, constants=const)\n",
    "    fig, all_axes = plt.subplots(2, 3, figsize=(14, 6))\n",
    "    for axes, odesys in zip(all_axes, [odes, odes.as_autonomous()]):\n",
    "        res = odesys.integrate(t, init_cond, params, integrator='cvode')\n",
    "        t_sec = to_unitless(res.xout, u.second)\n",
    "        FeSCN_ref = binary_rev(t_sec, kf_ref, kb_ref, 0, Fe0, SCN0)\n",
    "        cmp = to_unitless(res.yout, u.M)\n",
    "        ref = np.empty_like(cmp)\n",
    "        ref[:, odesys.names.index('FeSCN+2')] = FeSCN_ref\n",
    "        ref[:, odesys.names.index('Fe+3')] = Fe0 - FeSCN_ref\n",
    "        ref[:, odesys.names.index('SCN-')] = SCN0 - FeSCN_ref\n",
    "        axes[0].plot(t_sec, cmp)\n",
    "        axes[1].plot(t_sec, cmp - ref)\n",
    "        res.plot_invariant_violations(ax=axes[2])\n",
    "        assert np.allclose(cmp, ref)\n",
    "        print({k: v for k, v in res.info.items() if not k.startswith('internal')})    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integrate_and_plot(rsys1, {'temperature': T_K*u.K})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rsys2 = ReactionSystem.from_string(\"\"\"\n",
    "Fe+3 + SCN- -> FeSCN+2; MassAction(EyringHS([{dHf}*J/mol, {dSf}*J/K/mol]))\n",
    "FeSCN+2 -> Fe+3 + SCN-; MassAction(EyringHS([{dHb}*J/mol, {dSb}*J/K/mol]))\n",
    "\"\"\".format(dHf=dHf, dSf=dSf, dHb=dHb, dSb=dSb))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integrate_and_plot(rsys2, {'temperature': T_K*u.K})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rsys3 = ReactionSystem.from_string(\"\"\"\n",
    "Fe+3 + SCN- -> FeSCN+2; MassAction(EyringHS.fk('dHf', 'dSf'))\n",
    "FeSCN+2 -> Fe+3 + SCN-; MassAction(EyringHS.fk('dHb', 'dSb'))\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integrate_and_plot(rsys3, dict(temperature=T_K*u.K,\n",
    "                               dHf=dHf*u.J/u.mol, dSf=dSf*u.J/u.mol/u.K,\n",
    "                               dHb=dHb*u.J/u.mol, dSb=dSb*u.J/u.mol/u.K))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
